import { json, error } from '@sveltejs/kit'; import { createAirtableServices } from '$lib/airtable'; import type { RequestHandler } from './$types'; const apiKey = process.env.VITE_AIRTABLE_API_KEY; const baseId = process.env.VITE_AIRTABLE_BASE_ID; if (!apiKey || !baseId) { throw new Error('Airtable API key and base ID must be configured'); } const airtable = createAirtableServices(apiKey, baseId); // GET /api/settings/[id] - Get specific settings export const GET: RequestHandler = async ({ params }) => { try { const { id } = params; const record = await airtable.settings.getRecord(id); const settings = { id: record.id, ...record.fields, dashboardLayout: record.fields.dashboardLayout ? JSON.parse(record.fields.dashboardLayout) : null }; return json(settings); } catch (err) { console.error('Error fetching settings:', err); throw error(404, 'Settings not found'); } }; // PUT /api/settings/[id] - Update settings export const PUT: RequestHandler = async ({ params, request }) => { try { const { id } = params; const settingsData = await request.json(); const fields = { ...settingsData, dashboardLayout: settingsData.dashboardLayout ? JSON.stringify(settingsData.dashboardLayout) : null }; const record = await airtable.settings.updateRecord(id, fields); const settings = { id: record.id, ...record.fields, dashboardLayout: record.fields.dashboardLayout ? JSON.parse(record.fields.dashboardLayout) : null }; return json(settings); } catch (err) { console.error('Error updating settings:', err); throw error(500, 'Failed to update settings'); } }; // DELETE /api/settings/[id] - Delete settings export const DELETE: RequestHandler = async ({ params }) => { try { const { id } = params; await airtable.settings.deleteRecord(id); return json({ success: true }); } catch (err) { console.error('Error deleting settings:', err); throw error(500, 'Failed to delete settings'); } };